<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改信息</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <!-- 引入 Bootstrap -->
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<style>
    img{
        width: 150px;
        height: 150px;
    }
</style>
<div class="container">

    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>修改信息</small>
                </h1>
            </div>
        </div>
    </div>

    <form action="${pageContext.request.contextPath}/product/updateBook" method="post">
        <input type="hidden" name="pid" value="${product.getPid()}"/>
        书籍名称：<input type="text" name="pname" value="${product.getPname()}"/>
        书籍价格：<input type="text" name="price" value="${product.getPrice()}"/>
        <div class="submit">
            <a href="javascript:void(0)" id="selectPhoto" onclick="uploadPhoto()">更换图片</a>
            <img id="images" alt="" src="/images/${product.photo }">
            <input type="file" id="photoFile" style="display: none;" onchange="upload()">

            <img id="preview_photo" src="${product.getPhoto()}" width="100px" height="100px" hidden>

            <input type="text" name="photo" id="productImg" hidden>
        </div>
        书籍库存：<input type="text" name="inventory" value="${product.getInventory()}"/>
        书籍时间：<input type="text" name="update_date" value="${product.getUpdateTime()}"/>
        书籍内容：<input type="text" name="detail" value="${product.getDetail() }"/>
        <input type="submit" value="提交"/>
    </form>
</div>
<script>
    function uploadPhoto() {
        $("#photoFile").click();
    }
    function upload() {
        var aaa = new FormData();
        aaa.append('photo', document.getElementById('photoFile').files[0]);
        $.ajax({
            url:"${pageContext.request.contextPath}/uploadPhoto",
            type:"post",
            data: aaa,
            contentType: false,
            processData: false,
            success: function(data) {
                if (data.type == "success") {
                    $("#preview_photo").attr("src", data.filepath+data.filename);
                    $("#productImg").val(data.filename);
                    $("#preview_photo").show();
                } else {
                    alert(data.msg);
                }
            },
            error:function(data) {
                alert("上传失败")
            }
        });
    }
</script>
</body>
</html>